<?php
namespace Admin\Model;
use Think\Model;

class OrderModel extends Model {
    

    const ORDER_UNCONFIRMED = 0;
    const ORDER_CONFIRMED = 1;
    const ORDER_COMPLATED = 2;
    const ORDER_CANNELED = 3;
    const SHIPPING_UNSHIPPED = 0;
    const SHIPPING_PARTIALSHIPMENT = 1;
    const SHIPPING_SHIPPED = 2;
    const REVIEW_UNREVIEWED = 0;
    //--支付状态
//    const ORDER_REFUND_SUCCEED = 5; //退款成功
    const ORDER_REFUND_FAIL = 4;    //退款失败
    const ORDER_APPLY_REFUND = 3; //申请退款
    const ORDER_REFUND = 2;      //已退款
    const ORDER_PAY = 1;        //已付款
    const ORDER_UNPAY = 0;      //未付款
    const REVIEW_REVIEWED = 1;
    
    protected $_auto = array (
        array('uid', API_UID, self::MODEL_INSERT),
        array('order_status', 0, self::MODEL_INSERT),
        array('shipping_status', 0, self::MODEL_INSERT),
        array('review_status', 0, self::MODEL_INSERT),
        array('status', 0, self::MODEL_INSERT),
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('shipping_time', 0, self::MODEL_INSERT)
    );
    
    protected function build_order_no() {
        mt_srand((double) microtime() * 1000000);
        return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    }
    
    public function createOrder($carts, $receiver) {
        if (empty($carts)) {
            return false;
        }
        $orders = array();
        foreach ($carts as $cart) {
            $product_info = M('product')->where(array('status'=>1,'id'=>$cart['product_id']))->find();
            if ($product_info) {
                $product['product_id'] = $cart['product_id'];
                $product['product_title'] = $product_info['title'];
                if (intval($product_info['quantity']) - intval($cart['quantity']) < 0) {
                    $this->error = '商品【' . $product_info . '】库存不足';
                    return false;
                }
                $product['quantity'] = $cart['quantity'];
                $product['product_attr'] = '默认';
                $price = $product_info['price'];
                if ($cart['option_ids']) {
                    $option_ids = str2arr($cart['option_ids'], ',');
                    sort($option_ids, SORT_NUMERIC);
                    $product_attr_text = '';
                    foreach ($option_ids as $option_id) {
                        $option_pid = get_table_field($option_id, 'id', 'pid', 'product_option');
                        $product_attr_text .= get_table_field($option_pid, 'id', 'title', 'product_option') . ':';
                        $product_attr_text .= get_table_field($option_id, 'id', 'title', 'product_option') . ',';
                    }
                    $product['product_attr'] = substr($product_attr_text, 0, strlen($product_attr_text) - 1);
                    $option_ids = arr2str($option_ids, ',');
                    $product_option_price_info = M('product_option_price')->where(array('product_id'=>$cart['product_id'], 'product_option_id'=>$option_ids))->find();
                    if ($product_option_price_info) {
                        $price = $product_option_price_info['price'];
                    }
                }
                $product_total_price = floor($price) * floor($cart['quantity']);;
                $product['price'] = $product_total_price;
                $product['option_ids'] = $cart['option_ids'];
                $product['unit_price'] = $price;
                $product['shipping_status'] = 0;
                $orders[$product_info['uid']]['products'][] = $product;
                if (!isset($orders[$product_info['uid']])) {
                    $orders[$product_info['uid']]['price'] = 0;
                }
                $orders[$product_info['uid']]['price'] = floor($orders[$product_info['uid']]['price']) + $product_total_price;
            } else {
                $this->error = '您购买的商品【' . $product_info['title'] . '】已下架';
                return false;
            }
        }
        
        $returns = array();
        foreach ($orders as $order) {
            $products = $order['products'];
            $data = $this->create();
            $orderno = $this->build_order_no();
            $data['order_no'] = $orderno;
            $data['price'] = $order['price'];
            $data['consignee'] = $receiver['realname'];
            $data['mobile'] = $receiver['mobile'];
            $data['province_id'] = $receiver['province_id'];
            $data['city_id'] = $receiver['city_id'];
            $data['area_id'] = $receiver['area_id'];
            $data['address'] = $receiver['address'];
            $res = $id = $this->add($data);
            if ($res === false) {
                $this->error = '未知错误';
                return false;
            }
            $returns[] = array('id'=>$id,'order_no'=>$orderno);
            foreach ($carts as $cart) {
                M('cart')->where(array('id'=>$cart['id']))->delete();
            }
            foreach ($products as $product) {
                $product['order_id'] = $id;
                M('order_product')->add($product);
                M('product')->where(array('id'=>$product['product_id']))->setDec(intval($product['quantity']));
            }
        }
        
        return $returns;
    }
    
    public function getOrderStatusText($order) {
        if($order['order_status'] == self::ORDER_CANNELED){
            return '交易取消';
        }else
        if($order['pay_status']==self::ORDER_UNPAY){
            return '待付款';
        }elseif ($order['pay_status']==self::ORDER_APPLY_REFUND) {
            return '申请退款';
        }elseif($order['pay_status']==self::ORDER_REFUND){
            return '已退款';
        }
        else
            if ($order['order_status'] == self::ORDER_COMPLATED && $order['shipping_status'] == self::SHIPPING_SHIPPED && $order['review_status'] == self::REVIEW_REVIEWED) {
                return '交易完成';
            } elseif ($order['order_status'] == self::ORDER_COMPLATED && $order['shipping_status'] == self::SHIPPING_SHIPPED && $order['review_status'] == self::REVIEW_UNREVIEWED) {
                return '待评价';
            } elseif ($order['order_status'] == self::ORDER_CONFIRMED && $order['shipping_status'] == self::SHIPPING_SHIPPED) {
                return '待收货';
            } elseif ($order['order_status'] == self::ORDER_CONFIRMED && $order['shipping_status'] != self::SHIPPING_SHIPPED) {
                return '待发货';
            }
            else {
                return '待发货';
            }
    }
}